package com.fadadianqi.search;

import com.yeqiangwei.commons.util.Pagination;
import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.BooleanClause.Occur;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.Sort;

import java.util.ArrayList;
import java.util.List;

public class Argument implements java.io.Serializable {

    /**
     *
     */
    private static final long serialVersionUID = -8792832364643540592L;

    private List<String> keywords = null;
    private List<String> fields = null;
    private List<BooleanClause.Occur> flags = null;
    private List<Float> boosts = null;
    private int page = 1;
    private int maxResults = 10;
    private boolean highlight = true;
    private Sort sort;

    public List<Query> queryList = new ArrayList<Query>();

    public void addQuery(Query query) {
        if (query != null)
            queryList.add(query);
    }

    public List<Query> getQueryList() {
        return queryList;
    }

    /* 是否给英文关键词加引号 */
    private boolean quote_en_keywords = false;

    private Pagination pagination;

    private Argument(List<String> keywords, List<String> fields
            , List<BooleanClause.Occur> flags
            , List<Float> boosts) {
        this.keywords = keywords;
        this.fields = fields;
        this.flags = flags;
        this.boosts = boosts;
    }

    public static Argument getInstance() {
        List<String> keywords = new ArrayList<String>();
        List<String> fields = new ArrayList<String>();
        List<BooleanClause.Occur> flags = new ArrayList<BooleanClause.Occur>();
        List<Float> boosts = new ArrayList<Float>();
        Argument arg = new Argument(keywords, fields, flags, boosts);
        return arg;
    }

    public Sort getSort() {
        return sort;
    }

    public void setSort(Sort sort) {
        this.sort = sort;
    }

    public final String[] getFieldArray() {
        if (fields == null) {
            return null;
        } else {
            String[] strs = new String[fields.size()];
            for (int i = 0; i < fields.size(); i++) {
                strs[i] = fields.get(i);
            }
            return strs;
        }
    }

    public final Float[] getBoostArray() {
        if (boosts == null) {
            return null;
        } else {
            Float[] strs = new Float[boosts.size()];
            for (int i = 0; i < boosts.size(); i++) {
                strs[i] = boosts.get(i);
            }
            return strs;
        }
    }

    public final Occur[] getOccurArray() {
        if (flags == null) {
            return null;
        } else {
            Occur[] strs = new Occur[flags.size()];
            for (int i = 0; i < flags.size(); i++) {
                strs[i] = flags.get(i);
            }
            return strs;
        }
    }

    public final String[] getKeywordArray() {
        if (keywords == null) {
            return null;
        } else {
            String[] strs = new String[keywords.size()];
            for (int i = 0; i < keywords.size(); i++) {
                strs[i] = keywords.get(i);
            }
            return strs;
        }
    }

    public Pagination getPagination() {
        return pagination;
    }

    public void setPagination(Pagination pagination) {
        this.pagination = pagination;
    }

    public boolean getQuote_en_keywords() {
        return quote_en_keywords;
    }

    public void setQuote_en_keywords(boolean quote_en_keywords) {
        this.quote_en_keywords = quote_en_keywords;
    }

    public boolean getHighlight() {
        return highlight;
    }

    public void setHighlight(boolean highlight) {
        this.highlight = highlight;
    }

    public void setArgs(String field, String key, BooleanClause.Occur flag, Float boost) {
        this.keywords.add(key);
        this.fields.add(field);
        this.flags.add(flag);
        this.boosts.add(boost);
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getMaxResults() {
        return maxResults;
    }

    public void setMaxResults(int maxResults) {
        this.maxResults = maxResults;
    }

    public List<String> getKeywords() {
        return keywords;
    }

    public List<String> getFields() {
        return fields;
    }

    public List<BooleanClause.Occur> getFlags() {
        return flags;
    }

    public List<Float> getBoosts() {
        return boosts;
    }
}
